package com.zllkj.mm.api.net;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import java.util.Arrays;
import java.util.Map;

/**
 * Created by admin on 2016/12/5.
 */

public class NetTools {

    /**
     * 签名
     *此版本未使用此签名方法
     * @param app_secret
     * @param paramMap
     * @return
     */
    public static String sign(String app_secret, Map<String, String> paramMap){
        paramMap.put("app_secret",app_secret);
        // 参数名字典排序
        String[] keyArray = paramMap.keySet().toArray(new String[0]);
        Arrays.sort(keyArray);
        // 拼接参数
        StringBuilder stringBuilder = new StringBuilder();
        for (String key : keyArray)  {
            stringBuilder.append(key).append("=").append(paramMap.get(key)).append("&");
        }
        //删除字符串最后一个"&"
        String codes = stringBuilder.toString().substring(0,stringBuilder.length()-1);
        // SHA-1签名
        String sign = /*new String(Hex.encodeHex(DigestUtils.sha(codes)))*/"";
        return sign;
    }

    /**
     * 判断网络是否可用
     *
     * @param context Context对象
     */
    public static Boolean isNetworkReachable(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo current = cm.getActiveNetworkInfo();
        if (current == null) {
            return false;
        }
        return (current.isAvailable());
    }
}
